define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'caijishuju/index' + location.search,
                    add_url: 'caijishuju/add',
                    edit_url: 'caijishuju/edit',
                    del_url: 'caijishuju/del',
                    multi_url: 'caijishuju/multi',
                    import_url: 'caijishuju/import',
                    table: 'caijishuju',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'expressno', title: __('Expressno'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('状态'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons: [
                        {
                            name: 'detail',
                            text: __('查询单号'),
                            title: __('查询单号'),
                            classname: 'btn btn-xs btn-primary btn-dialog',
                            icon: 'fa fa-list',
                            url: 'caijishuju/check',
                            callback: function (data) {
                                Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                            },
                            visible: function (row) {
                                //返回true时按钮显示,返回false隐藏
                                return true;
                            }
                        },
                    ],
                    formatter: Table.api.formatter.operate}
                    ]
                ]
            });
                // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                var ids = Table.api.selectedids(table);
                Fast.api.open("caijishuju/check?ids="+ids, "测试"); 
                // Layer.alert(JSON.stringify(Table.api.selectedids(table)));
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});